package com.jiantong.user.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.impl.dao.ButcherFactoryDao;
import com.jiantong.user.model.ButcherFactory;
import com.jiantong.user.service.ButcherFactoryReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 屠宰场
 * Created by mz03 on 2017/4/25.
 */
@Slf4j
@Service
@RpcProvider
public class ButcherFactoryReadServiceImpl implements ButcherFactoryReadService{



    private final ButcherFactoryDao butcherFactoryDao;

    @Autowired
    public ButcherFactoryReadServiceImpl (ButcherFactoryDao butcherFactoryDao){
        this.butcherFactoryDao = butcherFactoryDao;
    }

    @Override
    public Response<List<ButcherFactory>> findByCityId(LoginUser loginUser) {
        Integer provinceId = loginUser.getUserProfile().getProvinceId();
        Integer cityId = loginUser.getUserProfile().getCityId();
        Integer regionId = loginUser.getUserProfile().getRegionId();
        try {
            Map<String, Object> criteria = Maps.newHashMap();
            if (provinceId != null && cityId !=null && regionId != null) {
                criteria.put("provinceId", provinceId);
                criteria.put("cityId", cityId);
//                criteria.put("regionId",regionId);
            }
            List<ButcherFactory> breedFactoryList = butcherFactoryDao.findByCityId(criteria);
            if (breedFactoryList == null){
                log.error("failed to find breedFactoryList by provinceId = {}; cityId = {},regionId = {}",provinceId,cityId,regionId);
                return Response.fail("breedFactoryList.not.found");
            }
            return Response.ok(breedFactoryList);
        }catch (Exception e){
            log.error("failed to find breedFactoryList by provinceId = {}; cityId = {}, cause:{}", provinceId,cityId, Throwables.getStackTraceAsString(e));
            return Response.fail("BreedRecord.find.fail");
        }
    }

    @Override
    public Response<ButcherFactory> findButcherFactoryById(Long id) {
        try{
            ButcherFactory butcherFactory = butcherFactoryDao.findById(id);
            if(butcherFactory == null){
                log.error("failed.to.find.butcherFactory id ={} , case = {}",id);
                return Response.fail("failed.to.find.butcherFactory");
            }
            return Response.ok(butcherFactory);
        }catch (Exception e){
            log.error("failed.to.find.butcherFactory id ={} , case = {}",id);
            return Response.fail("failed.to.find.butcherFactory");
        }
    }

    /**
     * 查询全部屠宰场（status=1）
     * @return
     */
    @Override
    public Response<Paging<ButcherFactory>> findAllButcherFactory() {
        Map criteria = new HashMap();
        criteria.put("status",1);
        return Response.ok(butcherFactoryDao.pagingCommon(0,1000,criteria));
    }

    @Override
    public Response<List<String>> findAllCity() {
        return Response.ok(butcherFactoryDao.findAllCity());
    }

    @Override
    public Response<List<String>> findRegionByCity(String city) {

        return Response.ok(butcherFactoryDao.findRegionByCity(city));
    }

    @Override
    public Response<List<ButcherFactory>> findByRegion(String region) {
        return Response.ok(butcherFactoryDao.findByRegion(region));
    }

    @Override
    public Response<List<ButcherFactory>> findByArea(Map<String, Object> map) {

        try{
            return Response.ok(butcherFactoryDao.findByArea(map));
        }catch (Exception e){
            log.error("failed to find by area",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find by area");
        }
    }

    @Override
    public Response<List<ButcherFactory>> findByIds(List<Long> butcherFactoryIds) {
        try{
            return Response.ok(butcherFactoryDao.findByIds(butcherFactoryIds));
        }catch (Exception e){
            log.error("failed to find by area",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find by area");
        }
    }
}
